﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using FTchina;

public partial class UI_sms_pshenhe : System.Web.UI.Page
{


    string shenhesql = "update sms_log set shenhe={0} where id in ({1})";//审核操作的sql
    string laqusql = "update sms_log set taskstate={0} , lockguid='' where id in ({1})";//重送操作的sql
    string DeleteSms_sended = "delete from sms_sended  where smsguid in (select top 1 smsguid from sms_log where id in  ({0}) )";//删除已发送列表

    string ReSendable = "select top 1 * from sms_log where shenhe<>0 and state<>'1' and id='{0}'";//审核通过或者自动放行，并且（发送失败或者未发送）
    string AlertMessage = "编号{0}的短信操作{1}";//操作结果提示信息
    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());


        string ids = FTchina.basic.SafeRequest(Request["ids"]);

        string op = FTchina.basic.SafeRequest(Request["op"]);

        if (ids.Length == 0) { GoBack(); }
        if (op.Length == 0) { GoBack(); }

        MainDo(ids, op);
        GoBack();
    }
    protected void MainDo(string ids, string op)
    {
        ArrayList al = new ArrayList();//操作结果的提示集合
        if (ids.Contains(','))
        {
            string[] idsList = ids.Split(',');
            foreach (string str in idsList)
            {
                if (str.Trim().Length > 0)
                {
                    al.Add(ExecList(str, op));
                }
            }
        }
        else
        {
            al.Add(ExecList(ids, op));
        }


        //将操作结果alert出来
        string tmp = string.Empty;
        foreach (string str in al)
        {
            tmp += str + "\\n";//弹出信息换行
        }
        //  tmp = tmp.Substring(0, tmp.Length - 2);
        // LogNet.LogBLL.debug(tmp);
        // JS.Alert(tmp);
    }


    /// <summary>
    /// 执行结果的集合
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected String ExecList(string ids, string op)
    {
        string tmp = string.Empty;


        if (ExecOpSqlSingleByID(ids, op))
        {
            tmp = string.Format(AlertMessage, ids, "成功！");
            LogNet.LogBLL.info(FTchina.Library.MasterHelper.MasterName + "操作编号" + ids + "的短信为" + op + "成功");
        }
        else
        {
            tmp = string.Format(AlertMessage, ids, "失败！");
            LogNet.LogBLL.info(FTchina.Library.MasterHelper.MasterName + "操作编号" + ids + "的短信为" + op + "失败");
        }
        return tmp;
    }
    /// <summary>
    /// 执行单个操作命令
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected Boolean ExecOpSqlSingleByID(string ids, string op)
    {
        #region 额外添加的处理，重新发送的时候将sms_sended中的数据一并删除
        if (op == "lok")
        {
            if (CheckResendable(ids))
            {//先判断是否能够重送
                shujuku.ExecuteSql(string.Format(DeleteSms_sended, ids));
            }
        }
        #endregion

        return ExecSqls(MakeSqls(ids, op));
    }

    /// <summary>
    /// 得到sql语句
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="tb"></param>
    /// <param name="op"></param>
    /// <returns></returns>
    protected string MakeSqls(string ids, string op)
    {
        string tmp = string.Empty;
        switch (op)
        {
            case "sok":
                {//审核通过
                    tmp = string.Format(shenhesql, "1", ids);
                    break;
                }
            case "sno":
                {//审核不通过

                    tmp = string.Format(shenhesql, "0", ids);
                    break;
                }
            case "lok":
                {//重新拉取
                    if (CheckResendable(ids))
                    {//先判断是否能够重送
                        tmp = string.Format(laqusql, "2", ids);
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
        //  LogNet.LogBLL.debug(tmp);
        return tmp;
    }
    /// <summary>
    /// 执行sql语句
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    protected Boolean ExecSqls(string sql)
    {
        if (sql.Length > 0)
        {
            //LogNet.LogBLL.info(sql);
            //return true;
            return shujuku.ExecuteSql(sql);
        }
        else
        {
            return false;
        }
    }


    /// <summary>
    /// 返回上一页
    /// </summary>
    protected void GoBack()
    {
        string back = FTchina.basic.SafeRequest(Request["back"]);
        if (back.Length > 0)
        {
            JS.JavaScriptLocationHref(back);
        }
        else
        {
            JS.GoHistory(-1);
        }
    }




    /// <summary>
    /// 查看是否能够重送，发送失败或者未发送，并且审核通过的。
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    private Boolean CheckResendable(string ids)
    {
        string sql = string.Format(ReSendable, ids);
        return shujuku.testread(sql);
    }

}